package com.cg.asm.core.config.security;

import com.alibaba.fastjson.JSONObject;
import com.cg.asm.core.dao.user.UserMapper;
import com.cg.asm.core.exception.AccountLockException;
import com.cg.asm.core.util.AsmApiUtils;
import com.cg.asm.client.CommonUtil;
import com.cg.asm.client.DateUtil;
import com.cg.asm.client.constans.ConstantsEnum;
import com.github.pagehelper.util.StringUtil;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class ResultAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Resource
    private UserMapper userMapper;

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter out = httpServletResponse.getWriter();

        JSONObject resultErrorInfo;
        if (e instanceof UsernameNotFoundException) {
            String returnCode = ConstantsEnum.RESPONSE_E_BAAC.getKey();
            String returnMsg = ConstantsEnum.RESPONSE_E_BAAC.getValue();
            resultErrorInfo = CommonUtil.errorJson(returnCode, returnMsg);
        } else if (e instanceof BadCredentialsException) {
            String username = httpServletRequest.getParameter("username");
            if(StringUtil.isNotEmpty(username)){
                userMapper.updateLockNumber(username, DateUtil.getDate("yyyy-MM-dd"));
            }
            String returnCode = ConstantsEnum.RESPONSE_E_BAAD.getKey();
            String returnMsg = ConstantsEnum.RESPONSE_E_BAAD.getValue();
            resultErrorInfo = CommonUtil.errorJson(returnCode, returnMsg);
        } else if (e instanceof DisabledException) {
            String returnCode = ConstantsEnum.RESPONSE_E_BAAF.getKey();
            String returnMsg = ConstantsEnum.RESPONSE_E_BAAF.getValue();
            resultErrorInfo = CommonUtil.errorJson(returnCode, returnMsg);
        } else if (e instanceof SessionAuthenticationException) {
            String returnCode = ConstantsEnum.RESPONSE_E_BAAB.getKey();
            String returnMsg = ConstantsEnum.RESPONSE_E_BAAB.getValue();
            resultErrorInfo = CommonUtil.errorJson(returnCode, returnMsg);
        }  else {
            if (e.getCause() instanceof AccountLockException) {
                String returnCode = ConstantsEnum.RESPONSE_E_BAAE.getKey();
                String returnMsg = ConstantsEnum.RESPONSE_E_BAAE.getValue();
                resultErrorInfo = CommonUtil.errorJson(returnCode, returnMsg);
            }else{
                String returnCode = ConstantsEnum.RESPONSE_E_AAAD.getKey();
                String returnMsg = ConstantsEnum.RESPONSE_E_AAAD.getValue();
                resultErrorInfo = CommonUtil.errorJson(returnCode, returnMsg);
            }
        }

        AsmApiUtils.outPrintlnByJSONObject(out, resultErrorInfo);
    }
}
